package com.wjk.kylin.mall.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjk.kylin.mall.admin.entity.UserRoleEntity;
import com.wjk.kylin.mall.admin.mapper.UserRoleMapper;
import com.wjk.kylin.mall.admin.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjk.kylin.mall.common.core.dto.admin.UserIdInDTO;
import com.wjk.kylin.mall.common.core.dto.base.BaseOutDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author wangjinkui
 * @since 2021-11-14
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEntity> implements IUserRoleService {

    @Override
    public DataOutDTO<Object> getRoleIdByUserId(UserIdInDTO inDTO) {
        List<Long> list = new ArrayList<>();

        QueryWrapper<UserRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", inDTO.getUserId());
        wrapper.select("role_id");
        List<UserRoleEntity> entityList = baseMapper.selectList(wrapper);

        if(CollectionUtil.isNotEmpty(entityList)){
            list = entityList.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toList());
        }
        return DataOutDTO.builder().data(list).build();
    }
}
